package com.vnp.cooking;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.vnp.cooking.bean.ObjCookpad;
import com.vnp.cooking.utils.KeyboardHelper;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class TaoMonAnCamNghiActivity extends Activity implements OnClickListener{
	
	private static final String SCREEN_LABEL = "Tao mon an cam nghi screen";
	
	ImageButton btnBack;
	Button btnDoneDoidieu;
	TextView titleScreen;
	EditText edtCamnghi;
	ObjCookpad objCookpad = new ObjCookpad();
	ImageView imgMonan;
	PreferenceHelper preferenceHelper;
	
	Dialog dChuapost;
	boolean isChange = false;
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			closeActivity();
		}
	};
	Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tao_mon_an_cam_nghi);
		
		preferenceHelper = new PreferenceHelper(this);
		
		titleScreen = (TextView) findViewById(R.id.txt_type_cookpad_title);
		imgMonan = (ImageView) findViewById(R.id.img_bg_camnghi);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnDoneDoidieu = (Button) findViewById(R.id.btn_done_doidieu);
		edtCamnghi = (EditText) findViewById(R.id.edt_camnghi);
		
		
		// set hinh nen cho activity nay
		// bimatp factory
       /* BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 8;
		try {
			Uri uri = Uri.parse(objCookpad.getUrlImageCookpad());
			Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(),options);
			imgMonan.setImageBitmap(bitmap);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}*/
		
		edtCamnghi.requestFocus();
		KeyboardHelper.displaySoftKeyboard(getApplicationContext(), edtCamnghi);
		
		setFont();
		init();
		ApplicationContext.getGaTracker().set(Fields.SCREEN_NAME, SCREEN_LABEL);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		ApplicationContext.getGaTracker().send(MapBuilder.createAppView().build());
	}

	@Override
	public void onStop() {
		super.onStop();
	}
	
	private void setFont() {
		titleScreen.setTypeface(Utils.getTypefaceRegular(TaoMonAnCamNghiActivity.this));
		btnDoneDoidieu.setTypeface(Utils.getTypefaceRegular(TaoMonAnCamNghiActivity.this));
		edtCamnghi.setTypeface(Utils.getTypefaceRegular(TaoMonAnCamNghiActivity.this));
	}
	void init(){
		// setOnclick
		edtCamnghi.setText(preferenceHelper.getDescCT());
		edtCamnghi.setSelection(preferenceHelper.getDescCT().length());
		btnBack.setOnClickListener(this);
		btnDoneDoidieu.setOnClickListener(this);
		
		edtCamnghi.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					if (edtCamnghi.getText().toString().equals("")){
						Toast.makeText(TaoMonAnCamNghiActivity.this, "Không được để trống.", Toast.LENGTH_SHORT).show();
					} else {
						KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
						closeActivity();
						// truyen thong tin vua nhap ve trang truoc do tai day
						preferenceHelper.setDescCT(edtCamnghi.getText().toString());
					}
					
				}
				return false;
			}
		});
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			veDangCongthuc();
			break;

		case R.id.btn_done_doidieu:
			if (edtCamnghi.getText().toString().equals("")) {
				KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
				Toast.makeText(TaoMonAnCamNghiActivity.this, "Không được để trống.", Toast.LENGTH_SHORT).show();
			} else {
				KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
				closeActivity();
				// truyen thong tin vua nhap ve trang truoc do tai day
				preferenceHelper.setDescCT(edtCamnghi.getText().toString());
			}
			break;
		}
	}
	
	
	public void checkCamnghiNextStep() {
		if (TextUtils.isEmpty(edtCamnghi.getText().toString())) {
			Toast.makeText(TaoMonAnCamNghiActivity.this, "Báº¡n chÆ°a nháº­p mÃ´ táº£ cá»§a báº¡n", Toast.LENGTH_SHORT).show();
		} else {
			objCookpad.setDescriptionCookpad(edtCamnghi.getText().toString());
			// truyen du lieu tu ben chup anh sang
			Intent t = new Intent(TaoMonAnCamNghiActivity.this, TaoMonAnNguyenLieuActivity.class);
			t.putExtra("objCookpad", objCookpad);
			startActivity(t);
		}
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		veDangCongthuc();
	}
	
	void showDialogCanhbaochuapost() {
		dChuapost = new Dialog(TaoMonAnCamNghiActivity.this, R.style.DialogSlideAnimLeft);
		dChuapost.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dChuapost.setContentView(R.layout.dialog_thoat_process);

		// khai bao cac thanh phan
		TextView txtArlert = (TextView) dChuapost.findViewById(R.id.str_canhbao_thoat);
		Button btnCapnhatThoat = (Button) dChuapost.findViewById(R.id.btn_thoat);
		Button btnKoCapnhatThoat = (Button) dChuapost.findViewById(R.id.btn_ko_thoat);
		
		txtArlert.setText("Bạn chưa cập nhật cảm nghĩ của bạn. Bạn có muốn cập nhật và thoát?");
		btnCapnhatThoat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dChuapost.dismiss();
				preferenceHelper.setDescCT(edtCamnghi.getText().toString());
				closeActivity();
			}
		});
		
		btnKoCapnhatThoat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dChuapost.dismiss();
				mHandler.postDelayed(r, 300);
			}
		});

		dChuapost.setCanceledOnTouchOutside(true);
		dChuapost.show();
		dChuapost.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dChuapost.getWindow().setGravity(Gravity.CENTER);
	}
	
	void veDangCongthuc(){
		if (edtCamnghi.getText().toString().equals("")) {
			KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
//			Toast.makeText(TaoMonAnCamNghiActivity.this, "Không được để trống.", Toast.LENGTH_SHORT).show();
			preferenceHelper.setDescCT("");
			closeActivity();
		} else {
			if (!preferenceHelper.getDescCT().matches(edtCamnghi.getText().toString())) {
				KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
				showDialogCanhbaochuapost();
			} else {
				KeyboardHelper.hideSoftKeyboard(TaoMonAnCamNghiActivity.this);
				closeActivity();
			}
		}
	}

}
